<template>
  <div>
    <p class="tip">最大高度，通过设置 <table-api-link prop="max-height"/> 启用，当数据少时自适应</p>

    <vhb-table
      border
      resizable
      show-overflow
      ref="xTable1"
      max-height="400"
      :loading="loading">
      <vhb-column type="checkbox" width="60"></vhb-column>
      <vhb-column type="seq" width="100"></vhb-column>
      <vhb-column field="name" title="Name" sortable width="200"></vhb-column>
      <vhb-column field="age" title="Age" width="200"></vhb-column>
      <vhb-column field="sex" title="Sex" width="200"></vhb-column>
      <vhb-column field="region" title="Region" width="200"></vhb-column>
      <vhb-column field="address" title="Address" width="300" show-overflow></vhb-column>
      <vhb-column field="updateTime" title="UpdateTime" width="200"></vhb-column>
      <vhb-column field="createTime" title="CreateTime" width="200"></vhb-column>
    </vhb-table>

    <p class="demo-code">{{ $t('app.body.button.showCode') }}</p>

    <pre>
      <pre-code class="xml">{{ demoCodes[0] }}</pre-code>
      <pre-code class="javascript">{{ demoCodes[1] }}</pre-code>
    </pre>

    <p class="tip">当数据超过最大高度时自动显示滚动条</p>

    <vhb-table
      border
      resizable
      show-overflow
      ref="xTable2"
      max-height="400"
      :loading="loading">
      <vhb-column type="checkbox" width="60"></vhb-column>
      <vhb-column type="seq" width="100"></vhb-column>
      <vhb-column field="name" title="Name" sortable width="200"></vhb-column>
      <vhb-column field="age" title="Age" width="200"></vhb-column>
      <vhb-column field="rate" title="Rate" width="200"></vhb-column>
      <vhb-column field="time" title="Time" width="200"></vhb-column>
      <vhb-column field="updateTime" title="UpdateTime" width="200"></vhb-column>
      <vhb-column field="createTime" title="CreateTime" width="200"></vhb-column>
    </vhb-table>

    <p class="demo-code">{{ $t('app.body.button.showCode') }}</p>

    <pre>
      <pre-code class="xml">{{ demoCodes[2] }}</pre-code>
      <pre-code class="javascript">{{ demoCodes[3] }}</pre-code>
    </pre>
  </div>
</template>

<script>
export default {
  data () {
    return {
      loading: false,
      demoCodes: [
        `
        <vhb-table
          border
          resizable
          show-overflow
          ref="xTable"
          max-height="400"
          :loading="loading">
          <vhb-column type="checkbox" width="60"></vhb-column>
          <vhb-column type="seq" width="100"></vhb-column>
          <vhb-column field="name" title="Name" sortable width="200"></vhb-column>
          <vhb-column field="age" title="Age" width="200"></vhb-column>
          <vhb-column field="region" title="Region" width="200"></vhb-column>
          <vhb-column field="address" title="Address" width="300" show-overflow></vhb-column>
          <vhb-column field="updateTime" title="UpdateTime" width="200"></vhb-column>
          <vhb-column field="createTime" title="CreateTime" width="200"></vhb-column>
        </vhb-table>
        `,
        `
        export default {
          data () {
            return {
              loading: false
            }
          },
          created () {
            this.findList()
          },
          methods: {
            findList () {
              this.loading = true
              return new Promise(resolve => {
                setTimeout(() => {
                  let tableData = this.mockList(3)
                  // 阻断 vue 对大数组的监听，避免 vue 绑定大数据造成短暂的卡顿
                  if (this.$refs.xTable) {
                    this.$refs.xTable.loadData(tableData)
                  }
                  resolve()
                  this.loading = false
                }, 300)
              })
            },
            mockList (size) {
              const list = []
              for (let index = 0; index < size; index++) {
                list.push({
                  name: \`名称\${index}\`,
                  sex: '0',
                  num: 123,
                  age: 18,
                  num2: 234,
                  rate: 3,
                  address: 'shenzhen'
                })
              }
              return list
            }
          }
        }
        `,
        `
        <vhb-table
          border
          resizable
          show-overflow
          ref="xTable"
          max-height="400"
          :loading="loading">
          <vhb-column type="checkbox" width="60"></vhb-column>
          <vhb-column type="seq" width="100"></vhb-column>
          <vhb-column field="name" title="Name" sortable width="200"></vhb-column>
          <vhb-column field="age" title="Age" width="200"></vhb-column>
          <vhb-column field="sex" title="Sex" width="200"></vhb-column>
          <vhb-column field="rate" title="Rate" width="200"></vhb-column>
          <vhb-column field="time" title="Time" width="200"></vhb-column>
          <vhb-column field="updateTime" title="UpdateTime" width="200"></vhb-column>
          <vhb-column field="createTime" title="CreateTime" width="200"></vhb-column>
        </vhb-table>
        `,
        `
        export default {
          data () {
            return {
              loading: false
            }
          },
          created () {
            this.findList()
          },
          methods: {
            findList () {
              this.loading = true
              return new Promise(resolve => {
                setTimeout(() => {
                  let tableData = this.mockList(60)
                  // 阻断 vue 对大数组的监听，避免 vue 绑定大数据造成短暂的卡顿
                  if (this.$refs.xTable) {
                    this.$refs.xTable.loadData(tableData)
                  }
                  resolve()
                  this.loading = false
                }, 300)
              })
            },
            mockList (size) {
              const list = []
              for (let index = 0; index < size; index++) {
                list.push({
                  name: \`名称\${index}\`,
                  sex: '0',
                  num: 123,
                  age: 18,
                  num2: 234,
                  rate: 3,
                  address: 'shenzhen'
                })
              }
              return list
            }
          }
        }
        `
      ]
    }
  },
  created () {
    this.findList1()
    this.findList2()
  },
  methods: {
    findList1 () {
      this.loading = true
      return new Promise(resolve => {
        setTimeout(() => {
          const tableData = this.mockList(3)
          // 阻断 vue 对大数组的监听，避免 vue 绑定大数据造成短暂的卡顿
          if (this.$refs.xTable1) {
            this.$refs.xTable1.loadData(tableData)
          }
          resolve()
          this.loading = false
        }, 300)
      })
    },
    findList2 () {
      this.loading = true
      return new Promise(resolve => {
        setTimeout(() => {
          const tableData = this.mockList(60)
          // 阻断 vue 对大数组的监听，避免 vue 绑定大数据造成短暂的卡顿
          if (this.$refs.xTable2) {
            this.$refs.xTable2.loadData(tableData)
          }
          resolve()
          this.loading = false
        }, 300)
      })
    },
    mockList (size) {
      const list = []
      for (let index = 0; index < size; index++) {
        list.push({
          name: `名称${index}`,
          sex: '0',
          num: 123,
          age: 18,
          num2: 234,
          rate: 3,
          address: 'shenzhen'
        })
      }
      return list
    }
  }
}
</script>
